package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.PageBean;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.SpecTwo;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    @DubboReference
    private SpecificationService specificationService;

    @RequestMapping("specificationList")
    public String specificationList() {
        return "admin/specification";
    }

    @RequestMapping("findList")
    @ResponseBody
    public Result findList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize) {
        try {
            PageBean<TbSpecification> pageBean = specificationService.findList(pageNum,pageSize);
            return Result.ok().setData(pageBean);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }

    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public Result deletefication(Long[] id) {
        try {
            specificationService.deletefication(id);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(String listJson,TbSpecification tbSpecification) {
        try {
            List<TbSpecificationOption> options = JSONObject.parseArray(listJson, TbSpecificationOption.class);
            specificationService.addInfo(options,tbSpecification);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("updateById")
    @ResponseBody
    public Result updateById(Long id) {
        try {
            SpecTwo specTwo = specificationService.updateById(id);
            return Result.ok().setData(specTwo);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("updateSpec")
    @ResponseBody
    public Result updateSpec(String options,TbSpecification tbSpecification) {
        try {
            List<TbSpecificationOption> options1 = JSONObject.parseArray(options, TbSpecificationOption.class);
            specificationService.updateSpec(options1,tbSpecification);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }
}
